<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avator_box">
        <img src="../assets/vue-logo.png"
             alt="">
      </div>
      <el-form ref="loginFormRef"
               class="login_form"
               :model="loginForm"
               :rules="loginFormRules">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username"
                    prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password"
                    prefix-icon="el-icon-key"
                    type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="loginForm.checked">记住密码</el-checkbox>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary"
                     style="width:100%;"
                     @click="login">登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        username: '',
        password: '',
        checked: true
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login () {
      console.log(this)

      // 表单的预验证
      this.$refs.loginFormRef.validate(async valid => {
        console.log(valid)
        // 通过向后台发送请求获取验证数据
        // if (!valid) return
        // const {data:res} = await this.$axios.post('login', this.loginForm)
        // console.log(res)
        // 目前没有后台，直接跳转页面
        this.$message.success('登录成功')

        // 1. 登录成功后的 token， 保存到客户端 sessionStorage 中
        // console.log(res)
        // 2. 通过编程式导航跳转到主页 '/home'
        window.sessionStorage.setItem('token', 'res.data.token')
        this.$router.push({ path: '/home' })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: aquamarine;
  height: 100%;
  background: url("../assets/img/bg.jpg") no-repeat;
  background-position: center;
  background-size: cover;
}

.login_box {
  width: 350px;
  height: 400px;
  background: #fff;
  border-radius: 15px;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avator_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eeeeee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #dddddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    // background-color: #fff;
    img {
      height: 100%;
      width: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.login_form {
  position: absolute;
  bottom: 15px;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
